import { Toast } from 'antd-mobile';
import {
  registerAction,
  loginAction,
  getCurrentUserAction,
  getMemberListAction,
  updateCurrentUserAction,
  logoutAction,
  getSendedUserInfoAction,
  updateSendedUserInfoAction
} from '../services/user';

export default {

  namespace: 'user',

  state: {
    userInfo: {},
    memberList: [],
    sendedUserInfo: {}
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
    },
  },

  effects: {
    // 注册
    *register({ payload, cb }, { call, put }) {
      const res = yield call(registerAction, payload);
      const { code, msg } = res.data;
      if (code === 200) {
        yield put({
          type: 'getCurrentUser'
        });
        Toast.success(msg, 1, () => {
          cb && cb();
        });
      } else {
        // error info
        console.log(res);
      }
    },
    // 登录
    *login ({ payload, cb }, { call, put }) {
      const res = yield call(loginAction, payload);
      console.log(999, res);
      const { code, msg } = res.data;
      if (code === 200) {
        yield put({
          type: 'getCurrentUser'
        });
        Toast.success(msg, 1, () => {
          cb && cb();
        });
      } else {
        // error info
        console.log(res);
      }
    },
    // 获取当前用户信息
    *getCurrentUser (_, { call, put }) {
      const res = yield call(getCurrentUserAction);
      const { code, msg, data = {} } = res.data;
      if (code === 200) {
        yield put({
          type: 'save',
          payload: {
            userInfo: data
          }
        })
      } else {
        console.log(msg);
      }
    },
    // 更新当前登录用户信息
    *updateCurrentUser ({ payload, cb }, { call, put }) {
      const res = yield call(updateCurrentUserAction, payload);
      const { code, msg } = res.data;
      if (code === 200) {
        yield put({
          type: 'getCurrentUser'
        });
        cb && cb();
      } else {
        console.log(msg);
      }
    },
    // 获取对应的成员列表
    *getMemberList ({ payload }, { call, put }) {
      const res = yield call(getMemberListAction, payload);
      const { code, msg, data = [] } = res.data;
      if (code === 200) {
        yield put({
          type: 'save',
          payload: {
            memberList: data
          }
        })
      } else {
        console.log(msg);
      }
    },
    // 退出登录
    *logout ({ cb }, { call }) {
      const res = yield call(logoutAction);
      const { code, msg } = res.data;
      // 401：退出成功
      if (code === 401) {
        cb && cb();
      } else {
        console.log(msg);
      }
    },
    // 获取被发送者的信息
    *getSendedUserInfo ({ payload }, { call, put }) {
      const res = yield call(getSendedUserInfoAction, payload);
      const { code, msg, data = {} } = res.data;
      if (code === 200) {
        yield put({
          type: 'save',
          payload: {
            sendedUserInfo: data
          }
        });
      } else {
        console.log(msg);
      }
    },
    // 更新被发送者的信息
    *updateSendedUserInfo ({ payload }, { call, put }) {
      const res = yield call(updateSendedUserInfoAction, payload);
      const { code, msg } = res.data;
      if (code === 200) {
        yield put({
          type: 'getSendedUserInfo',
          payload: {
            sendedId: payload.toUserId
          }
        });
      } else {
        console.log(msg);
      }
    }
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    }
  }
};
